#include "stdafx.h"
#include "PositionAnimation.hpp"
#include <exception>
#include "ITransformable.hpp"

rh::PositionAnimation::PositionAnimation(rh::ITransformable* target, sf::Vector2f startPosition, sf::Vector2f endPosition, sf::Time animationTime)
	: Animation(target, animationTime, startPosition, endPosition)
{
	if(target == nullptr)
		throw std::invalid_argument("\"target\" property cannot be nullptr");

	_stepValue.x = (endPosition.x-startPosition.x)/animationTime.asMilliseconds();
	_stepValue.y = (endPosition.y-startPosition.y)/animationTime.asMilliseconds();
}


rh::PositionAnimation::~PositionAnimation(void)
{
}

void rh::PositionAnimation::Start(void)
{
	(*_target).setPosition(_startValue);
	rh::Animation<sf::Vector2f>::Start();
}

void rh::PositionAnimation::Animate(const sf::Time& elapsedTime)
{
	sf::Vector2f tempStep;
	tempStep.x = _startValue.x + _stepValue.x*_animationElapsedTime.asMilliseconds();
	tempStep.y = _startValue.y +_stepValue.y*_animationElapsedTime.asMilliseconds();
	(*_target).setPosition(tempStep);
}

void rh::PositionAnimation::CleanupAnimation(void)
{
	(*_target).setPosition(_endValue);
}